﻿using System;
using System.Collections.Generic;
using System.Text;


namespace dllLink
{
    // Classe représentant un produit
    // Un Product est composé d'Assets (au sens large)
    // La convention pour le tableau mvAssetList sera la suivante :
    // - d'abord les sous-jacents (OPCVM)
    // - puis le(s) taux de change (EXCHANGERATE)
    // - et enfin le(s) taux d'intérêt (INTERESTRATE)

    internal class Product
    {
        private DateTime[] mvFixingDates;
        private int mvNbFixingDates;
        private int mvNbAssets;
        private int mvNbUnderlying;
        private Asset[] mvAssetList;
        private double[] mvWeights;


        public Product(string[] theNamesList, string[] theTypesList, string[] theCurrenciesList, DateTime[] theFixingDates)
        {
            mvFixingDates = theFixingDates;
            mvNbFixingDates = theFixingDates.Length;
            mvNbAssets = theNamesList.Length;
            mvAssetList = new Asset[mvNbAssets];
            mvNbUnderlying = 0;
            for (int i = 0; i < mvNbAssets; i++)
            {
                mvAssetList[i] = new Asset(theNamesList[i],theTypesList[i],theCurrenciesList[i]);
                if (mvAssetList[i].isUnderlying())
                {
                    mvNbUnderlying++;
                }
            }
            mvWeights = new double[mvNbUnderlying];
            for (int i = 0; i < mvNbUnderlying; i++)
            {
                mvWeights[i] = 1.0 / (double)mvNbUnderlying;
            }
        }

        public DateTime[] mGetFixingDates()
        {
            return mvFixingDates;
        }

        public int mGetNbFixingDates()
        {
            return mvNbFixingDates;
        }

        public void mSetWeights(double[] theWeights)
        {
            mvWeights = theWeights;
        }

        public double[] mGetWeights()
        {
            return mvWeights;
        }

        public int mGetNbAssets()
        {
            return mvNbAssets;
        }

        public int mGetNbUnderlyings()
        {
            int res = 0;
            for (int i = 0; i < mvNbAssets; i++)
            {
                if (mvAssetList[i].isUnderlying())
                {
                    res++;
                }
            }
            return res;
        }


        public int mGetNbExchangeRates()
        {
            int res = 0;
            for (int i = 0; i < mvNbAssets; i++)
            {
                if (mvAssetList[i].isExchangeRate())
                {
                    res++;
                }
            }
            return res;
        }


        public int mGetNbInterestRates()
        {
            int res = 0;
            for (int i = 0; i < mvNbAssets; i++)
            {
                if (mvAssetList[i].isInterestRate())
                {
                    res++;
                }
            }
            return res;
        }


        public string mGetAssetName(int i)
        {
            if (i >=0 && i < mvNbAssets)
            {
                return mvAssetList[i].mGetName();
            }
            else
            {
                return "";
            }
        }


        public string mGetRateName(int i)
        {
            int myRateIndex = mvNbUnderlying+1;
            if (myRateIndex+i <= mvNbAssets)
            {
                return mvAssetList[myRateIndex+i].mGetName();
            }
            else
            {
                return "";
            }
        }


        public double mGetAssetNAV(int i, DateTime theT)
        {
            return mvAssetList[i].mNAV(theT);
        }

        public double[] mGetAssetVectNAV(int i, DateTime[] theDates)
        {
            return mvAssetList[i].mVectNAV(theDates);
        }

        public bool[] mUnderlyingIsUSD()
        {
            bool[] myUnderlyingIsUSD = new bool[mvNbUnderlying];
            for (int i = 0; i < mvNbUnderlying; i++)
            {
                myUnderlyingIsUSD[i] = mvAssetList[i].isUSD();
            }
            return myUnderlyingIsUSD;
        }
    }
}
